/**
 * Copyright (C) 2010 Hamburg Sud
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.hamburgsud.log4testing.core.data;

import java.util.ArrayList;

/**
 * A TestCase provides a List of TestSteps which are combined in this TestCase
 * Context of a TestCase: A TestCase contains many TestSteps, which will be
 * performed if this TestCase is used. So a TestCase aggregates a bunch of
 * TestSteps in his TestStepGroups.
 */
public class TestCase extends TestStepContainer {

	/**
	 * List of TestStepGroups which contains the TestSteps
	 */
	private ArrayList<TestStepGroup> testStepGroups = new ArrayList<TestStepGroup>();

	/**
	 * Constructor which just calls the superclass
	 * 
	 * @param name
	 *            Name of the TestCase
	 */
	public TestCase(final String name) {
		super(name);
	}

	/**
	 * Creates a new TestStepGroup with the given name
	 * 
	 * @param name
	 *            Name of the TestStepGroup
	 * @return TestStepGroup with the given name
	 */
	public TestStepGroup newTestStepGroup(final String name) {
		final TestStepGroup testStepGroup = new TestStepGroup(name);
		addTestStepGroup(testStepGroup);
		return testStepGroup;
	}

	/**
	 * Creates a new TestStep and calls addtestStep
	 * 
	 * @return TestStep which was created
	 */
	public TestStep newTestStep() {
		final TestStep testStep = new TestStep();
		addTestStep(testStep);
		return testStep;
	}

	/**
	 * Adds a TestStep to the TestStepGroup
	 * 
	 * @param testStep
	 *            testStep to add
	 * @return boolean - if the adding was successful
	 */
	public boolean addTestStep(final TestStep testStep) {
		boolean added = false;
		if (testStepGroups.size() > 0) {
			final TestStepGroup currentTestStepGroup = testStepGroups
					.get(testStepGroups.size() - 1);
			currentTestStepGroup.addTestStep(testStep);
			added = true;
		} else {
			added = false;
		}
		return added;
	}

	/**
	 * Creates a new TestStep with given parameters
	 * 
	 * @param status
	 *            TestStatus of the TestStep
	 * @param command
	 *            Command of the TestStep
	 * @param comment
	 *            Comment of the TestSTep
	 * @param message
	 *            Message of the TestStep
	 * @return TestStep which was created
	 */
	public TestStep newTestStep(final TestStatus status, final String command,
			final String comment, final String message) {
		final TestStep testStep = new TestStep();
		testStep.setCommand(command);
		testStep.setComment(comment);
		testStep.setMessage(message);
		testStep.setStatus(status);
		if (addTestStep(testStep)) {
			testStep.start();
			return testStep;
		} else {
			return null;
		}
	}

	/**
	 * Creates a new TestStep without a message
	 * 
	 * @param status
	 *            TestStatus of the TestStep
	 * @param command
	 *            Command of the TestStep
	 * @param comment
	 *            Comment of the TestSTep
	 * @return TestStep which was created
	 */
	public TestStep newTestStep(final TestStatus status, final String command,
			final String comment) {
		return newTestStep(status, command, comment, "");
	}

	/**
	 * Adds a given TestStepGroup to the list of TestStepGroups
	 * 
	 * @param testStepGroup
	 *            the TestStepGroup to add
	 */
	private TestCase addTestStepGroup(final TestStepGroup testStepGroup) {
		testStepGroups.add(testStepGroup);
		return this;
	}

	/**
	 * 
	 * @return the TestStepGroups Array as Iterable
	 */
	public Iterable<TestStepGroup> getTestStepGroups() {
		return testStepGroups;
	}

	/**
	 * Returns the number of the Failed TestSteps in the TestStepGroups
	 */
	@Override
	public int getNumberOfFailed() {
		int numberOfFailed = getNumberOfFailed(testStepGroups);
		return numberOfFailed;
	}

	/**
	 * Returns the number of all TestSteps in the TestStepGroups
	 */
	@Override
	public int getNumberOfTestSteps() {
		int numberOfTestSteps = getNumberOfTestSteps(testStepGroups);
		return numberOfTestSteps;
	}

	/**
	 * Returns the first failed TestStep from all TestStepGroups
	 */
	@Override
	public TestStep getFirstFailed() {
		final TestStep firstFailed = getFirstFailed(testStepGroups);
		return firstFailed;
	}

	/**
	 * Returns the first TestStep of all TestStepGroups
	 */
	@Override
	public TestStep getFirstTestStep() {
		final TestStep firstTestStep = getFirstTestStep(testStepGroups);
		return firstTestStep;
	}

	/**
	 * Returns the last TestStep of all TestStepGroups
	 */
	@Override
	public TestStep getLastTestStep() {
		final TestStep lastTestStep = getLastTestStep(testStepGroups);
		return lastTestStep;
	}

}
